﻿angular.module("umbraco").controller("UE.DealerCreateController",
    function ($scope, dealerResource, treeService, notificationsService, navigationService) {
        var vm = this;
        vm.buttonState = "init";
        $scope.dealer = {
            address: "", name: "", responsible: "", contact: ""
        };
        $scope.errorMsg = '';

        $scope.createDealer = function () {
            if ($scope.dealer.address == '') {
                $scope.errorMsg = "输入名字";
            } else {
                vm.buttonState = "busy";
                dealerResource.create($scope.dealer.address, $scope.dealer.name, $scope.dealer.responsible, $scope.dealer.contact).then(function (response) {
                    if (response.ErrorCode == 0) {
                        treeService.clearCache({ section: "dealer" });
                        notificationsService.success("Create Success", "Create dealer success!");
                        navigationService.changeSection("dealer");
                        navigationService.syncTree({
                            tree: 'dealerListAlias',
                            path: response.Extend,
                            forceReload: true,
                            activate: true
                        }).then(function (syncArgs) {
                            //console.log(syncArgs);
                        });
                    } else {
                        vm.buttonState = "init";
                        notificationsService.error("Create Failed", "Create failed, " + response.ErrorMsg + "!");
                    }
                });
            }
        }
    }
);